'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System.Collections.ObjectModel
Imports System.Linq

Public Class SignInPageViewModel
    Inherits System.Windows.Threading.DispatcherObject

    Private _Identities As ObservableCollection(Of IdentityManager.IIdentity)
    Private _IdentitiesView As System.ComponentModel.ICollectionView
    Private _binding As Core.NotifyingCollectionBinding(Of IdentityManager.IIdentity)

    Public Sub New()
        _Identities = New ObservableCollection(Of IdentityManager.IIdentity)(IdentityProvider.Identities.ToList)
        _binding = New Core.NotifyingCollectionBinding(Of IdentityManager.IIdentity)(IdentityProvider.Identities, _Identities)

        _IdentitiesView = CollectionViewSource.GetDefaultView(_Identities)
    End Sub

    Public ReadOnly Property IdentityProvider() As IdentityManager.IIdentityProvider
        Get
            Return ApplicationService.Current.IdentityProvider
        End Get
    End Property

    Public ReadOnly Property Identities() As System.ComponentModel.ICollectionView ' ReadOnlyObservableCollection(Of IdentityManager.IIdentity)
        Get
            Return _IdentitiesView
        End Get
    End Property

    Public Sub SignIn(ByVal identity As IdentityManager.IIdentity)
        Dim svConnect As IcqInterface.Interfaces.IConnector
        Dim task As SignInTask

        ApplicationService.Current.CreateContext(identity)

        svConnect = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IConnector)()

        AddHandler svConnect.SignInCompleted, AddressOf OnSignInCompleted
        AddHandler svConnect.SignInFailed, AddressOf OnSignInFailed

        task = New SignInTask
        task.Credential = New IcqInterface.PasswordCredential(identity.GetAttribute(IdentityAttributes.PasswordAttribute))
        Core.Kernel.TaskScheduler.RunAsync(task)

        NavigateToSigningInPage()
    End Sub

    Private Sub OnDisconnected(ByVal sender As Object, ByVal e As IcqInterface.Interfaces.DisconnectedEventArgs)
        MessageBox.Show(String.Format("Connection lost; Message: {0}; Expected: {1}", e.Message, e.IsExpected))
    End Sub

    Private Sub OnSignInCompleted(ByVal sender As Object, ByVal e As EventArgs)
        Try
            AddHandler ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IConnector).Disconnected, AddressOf OnDisconnected
            AddHandler ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IOfflineMessageService).AllOfflineMessagesReceived, AddressOf OnAllOfflineMessagesReceived

            MessageWindowViewModel.RegisterEventHandlers()

            Dispatcher.BeginInvoke(Windows.Threading.DispatcherPriority.Normal, New Action(AddressOf NavigateToContactsPage))

            ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IOfflineMessageService).RequestOfflineMessages()

            Core.Kernel.TaskScheduler.RunAsync(New UploadAvatarActivity)

            Core.Kernel.TaskScheduler.RunAsync(New RequestShortUserInfoTask)
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnSignInFailed(ByVal sender As Object, ByVal e As IcqInterface.Interfaces.SignInFailedEventArgs)
        Try
            ' TODO: Implement propper Sign In failed handling ...
            MessageBox.Show(e.Message, "SignIn failed", MessageBoxButton.OK, MessageBoxImage.Error)

            NavigateToSignInPage()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnAllOfflineMessagesReceived(ByVal sender As Object, ByVal e As EventArgs)
        Try
            ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IOfflineMessageService).DeleteOfflineMessages()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Public Sub NavigateToContactsPage()
        Core.Kernel.Services.GetService(Of INavigationService).NavigateToContactsPage()
    End Sub

    Public Sub NavigateToCreateIdentityPage()
        Core.Kernel.Services.GetService(Of INavigationService).NavigateToCreateIdentityPage()
    End Sub

    Public Sub NavigateToSigningInPage()
        Core.Kernel.Services.GetService(Of INavigationService).NavigateToSigningInPage()
    End Sub

    Public Sub NavigateToSignInPage()
        Core.Kernel.Services.GetService(Of INavigationService).NavigateToSignInPage()
    End Sub

    Public Sub NavigateToEditIdentityPage(ByVal identity As IdentityManager.IIdentity)
        Core.Kernel.Services.GetService(Of INavigationService).NavigateToEditIdentityPage(identity)
    End Sub
End Class
